# @Time    : 2022/2/12 21:28
# @Author  : junbaibai
# @File    : HoiFileDetail.py
import pathlib
import traceback

from PySide6 import QtGui
from PySide6.QtWidgets import QFrame, QHeaderView

from src.db.obj.save_node import SaveNode
from src.service import save_node_service
from src.ui.ui.HoiFileDetail_ui import Ui_Form
from src.utils.hoi_file_parser import HoiParser


class HoiFileDetail(QFrame, Ui_Form):

    def __init__(self, parent=None, hoi: SaveNode = None):
        super(HoiFileDetail, self).__init__(parent=parent)

        self.hoi: SaveNode = hoi

        self.setupUi(self)
        self.model = QtGui.QStandardItemModel()
        self.tableView.setModel(self.model)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.model.appendColumn([QtGui.QStandardItem(i) for i in self.hoi.__slots__])
        self.model.appendColumn([QtGui.QStandardItem(str(i)) for i in self.hoi.values])
        self.setWindowTitle(pathlib.Path(self.hoi.src_path).name)

        self.restore_hoi_btn.clicked.connect(self.restore_hoi)

    def restore_hoi(self):
        HoiParser.restore(self.hoi)
        node = SaveNode(node=self.hoi)
        node.init_update_time()

        save_node_service.update_node_time(node)
